package org.ilead.iserve.model;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;

import org.ilead.iserve.service.manager.UserManager;

/**
 * PrivateMessage entity. @author MyEclipse Persistence Tools
 */

@Entity
public class PrivateMessage implements java.io.Serializable {

	// Fields

	private Integer id;
	private String title;
	private String content;
	private Date sendTime;
	private Integer source;
	private Integer target;
	private String formatTime;
	/**
	 * 发件者的信件的状态，0为正常的状态，-1为已删除
	 */
	private Integer seState;
	/**
	 * 收件者的信件的状态，0为未读，1为已读,-1为已删除
	 */
	private Integer reState;
	/**
	 * 若信件是回复，则显示回复信件的Id
	 */
	private Integer replyId;
	
	private String fromName;
	
	private String targetName;

	// Constructors

	/** default constructor */
	public PrivateMessage() {
	}

	public PrivateMessage(Integer id, String title, String content,
			Date sendTime, Integer source, Integer target,
			Integer seState, Integer reState, Integer replyId) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.sendTime = sendTime;
		this.source = source;
		this.target = target;
		this.seState = seState;
		this.reState = reState;
		this.replyId = replyId;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getSendTime() {
		return this.sendTime;
	}

	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

	public Integer getSource() {
		return this.source;
	}

	public void setSource(Integer source) {
		this.source = source;
	}

	public Integer getSeState() {
		return seState;
	}

	public void setSeState(Integer seState) {
		this.seState = seState;
	}

	public Integer getReState() {
		return reState;
	}

	public void setReState(Integer reState) {
		this.reState = reState;
	}

	public Integer getTarget() {
		return this.target;
	}

	public void setTarget(Integer target) {
		this.target = target;
	}
	
	public Integer getReplyId() {
		return replyId;
	}

	public void setReplyId(Integer replyId) {
		this.replyId = replyId;
	}

	public String getFormatTime(){
		if(formatTime == null || "".equals(formatTime)){
			SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
			formatTime = format.format(sendTime);
		}
		return formatTime;
	}
	
	public String getFromName(){
		if(null == fromName || "".equals(fromName)){
			User user = UserManager.getInstance().findById(source);
			fromName = user != null ? user.getDisplayName() : "";
		}
		return fromName;
	}
	
	public String getTargetName(){
		if(null == targetName || "".equals(targetName)){
			User user = UserManager.getInstance().findById(target);
			targetName = user != null ? user.getDisplayName() : "";
		}
		return targetName;
	}
	

}